/**
 * 简历API接口测试端点
 * 用于测试简历相关的API接口功能
 */

import { validateResumeContent } from '../../database/services/resumeService';

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event);
    const { testType, data } = body;
    
    const results: any = {
      success: true,
      timestamp: new Date().toISOString(),
      tests: []
    };
    
    // 测试简历内容验证
    if (testType === 'validate' || testType === 'all') {
      const validateTests = [
        {
          name: '有效JSON内容',
          input: JSON.stringify({ name: '测试', phone: '123' }),
          expected: true
        },
        {
          name: '无效JSON内容',
          input: 'invalid json',
          expected: false
        },
        {
          name: '空字符串',
          input: '',
          expected: false
        },
        {
          name: 'null值',
          input: 'null',
          expected: true
        }
      ];
      
      for (const test of validateTests) {
        const result = validateResumeContent(test.input);
        const passed = result === test.expected;
        
        results.tests.push({
          type: 'validate',
          name: test.name,
          input: test.input,
          expected: test.expected,
          actual: result,
          passed
        });
      }
    }
    
    // 测试简历数据结构
    if (testType === 'structure' || testType === 'all') {
      const sampleResumeContent = {
        name: '张三',
        phone: '13800138000',
        email: 'zhangsan@example.com',
        hometown: '北京',
        currentResidence: '上海',
        jobIntention: '前端开发工程师',
        education: [{
          school: '北京大学',
          major: '计算机科学与技术',
          degree: '本科',
          startDate: '2018-09-01',
          endDate: '2022-06-30'
        }],
        workExperience: [{
          company: '测试公司',
          position: '前端开发工程师',
          startDate: '2022-07-01',
          endDate: '2024-01-01',
          description: '负责前端开发工作'
        }],
        projects: [],
        skills: [{
          description: '熟悉Vue.js, React等前端框架'
        }],
        selfEvaluation: '热爱编程，有良好的学习能力'
      };
      
      const contentString = JSON.stringify(sampleResumeContent);
      const isValid = validateResumeContent(contentString);
      
      results.tests.push({
        type: 'structure',
        name: '完整简历数据结构',
        input: '完整的简历JSON数据',
        expected: true,
        actual: isValid,
        passed: isValid,
        sampleData: sampleResumeContent
      });
    }
    
    // 测试API参数验证
    if (testType === 'api' || testType === 'all') {
      const apiTests = [
        {
          name: '有效创建请求',
          data: {
            title: '测试简历',
            content: JSON.stringify({ name: '测试' })
          },
          expected: 'valid'
        },
        {
          name: '缺少标题',
          data: {
            content: JSON.stringify({ name: '测试' })
          },
          expected: 'invalid'
        },
        {
          name: '缺少内容',
          data: {
            title: '测试简历'
          },
          expected: 'invalid'
        },
        {
          name: '无效JSON内容',
          data: {
            title: '测试简历',
            content: 'invalid json'
          },
          expected: 'invalid'
        }
      ];
      
      for (const test of apiTests) {
        let validationResult = 'valid';
        
        // 验证标题
        if (!test.data.title || typeof test.data.title !== 'string' || test.data.title.trim().length === 0) {
          validationResult = 'invalid';
        }
        
        // 验证内容
        if (!test.data.content || typeof test.data.content !== 'string' || test.data.content.trim().length === 0) {
          validationResult = 'invalid';
        } else if (!validateResumeContent(test.data.content)) {
          validationResult = 'invalid';
        }
        
        const passed = validationResult === test.expected;
        
        results.tests.push({
          type: 'api',
          name: test.name,
          input: test.data,
          expected: test.expected,
          actual: validationResult,
          passed
        });
      }
    }
    
    // 计算总体结果
    const totalTests = results.tests.length;
    const passedTests = results.tests.filter((test: any) => test.passed).length;
    const failedTests = totalTests - passedTests;
    
    results.summary = {
      total: totalTests,
      passed: passedTests,
      failed: failedTests,
      successRate: totalTests > 0 ? (passedTests / totalTests * 100).toFixed(2) + '%' : '0%'
    };
    
    results.success = failedTests === 0;
    
    return results;
    
  } catch (error: any) {
    console.error('API测试失败:', error);
    return {
      success: false,
      error: error.message,
      timestamp: new Date().toISOString()
    };
  }
});
